package cn.com.dashihui.api.common;

import java.text.DecimalFormat;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 系统常量获取<br/>
 * 从数据库t_sys_config表获取
 */
public class SysConfig {
	private static final String LEVEL2_LINE_CODE = "S0001";
	private static final String signSpoint = "S0002";//每日签到赠送积分
	private static final String signSpointExtra = "S0003";//累计签到8天的额外奖励积分
	private static final String regSpoint = "S0004";//注册成功奖励积分
	private static final String regJkpoint = "S0006";//注册成功奖励的抵扣券
	private static final String inviteSpoint = "S0007";//邀请成功奖励的积分
	private static final String inviteJkpoint = "S0008";//邀请成功奖励的抵扣券
	private static final String inviteJypoint = "S0009";//邀请成功奖励的惠券
	private static final String POSTAGE = "S0010";//邮费
	private static final String POSTAGEFREE = "S0011";//免邮费金额
	private static final String GROUPSPPOINT = "S0012";//团中团邀请赠送积分
	private static final String GROUPSIZE = "S0013";//团中团显示个数
	private static final String SCALE1 = "S0014";//惠券转余额比例
	private static final String DEFAULTINVITE = "S0015";//默认推荐人手机号
	private static final String MININUMMONEY = "S0016";//开团资格最低消费
	private static final String rechargeAward = "P0001";//充值奖励比例
	
	
	private static final String GROUPMAXBUY = "S0017";//同一团购商品，每人每天限购次数
	private static final String GROUPDOWNGIVED = "S0018";//团购结束，给予没免单用户  代金券比例
	private static final String GROUPDOWNGIVEJ = "S0019";//团购结束，给予没免单用户  积分比例
	
	
	/**
	 * 获取团购结束，给予没免单用户  积分比例
	 * @return
	 * @throws Exception
	 */
	public static double getGroupDownGiveJ() throws Exception {
		return (double)getFinalValue(GROUPDOWNGIVEJ);
	}
	/**
	 * 获取团购结束，给予没免单用户  代金券比例
	 * @return
	 * @throws Exception
	 */
	public static double getGroupDownGiveD() throws Exception {
		return (double)getFinalValue(GROUPDOWNGIVED);
	}
	/**
	 * 获取同一团购商品，每人每天限购次数
	 * @return
	 * @throws Exception
	 */
	public static double getGroupMaxBuy() throws Exception {
		return (double)getFinalValue(GROUPMAXBUY);
	}
	/**
	 * 获取开团资格最低消费
	 * @return
	 * @throws Exception
	 */
	public static double getMiniNumMoney() throws Exception {
		return (double)getFinalValue(MININUMMONEY);
	}
	
	/**
	 * 获取充值奖励编码
	 * @return
	 * @throws Exception
	 */
	public static String getRechargeAwardCode() throws Exception {
		return rechargeAward;
	}
	
	/**
	 * 获取充值奖励
	 * @return
	 * @throws Exception
	 */
	public static double getRechargeAward() throws Exception {
		return (double)getFinalValue(rechargeAward);
	}
	
	/**
	 * 默认推荐人手机号
	 * @return
	 * @throws Exception
	 */
	public static String getDefaultInvite() throws Exception {
		Double defaultInvite=(Double) getFinalValue(DEFAULTINVITE);
		DecimalFormat df =new DecimalFormat("#");
		return  df.format(defaultInvite);
	}
	/**
	 * 惠券转换余额比例
	 * @return
	 * @throws Exception
	 */
	public static double getscale1() throws Exception {
		return (double)getFinalValue(SCALE1);
	}
	/**
	 * 团中团显示个数
	 */
	public static double getGroupSize() throws Exception {
		return (double)getFinalValue(GROUPSIZE);
	}
	/**
	 * 团中团邀请赠送积分
	 * @return
	 * @throws Exception
	 */
	public static double getGroupSppoint() throws Exception {
		return (double)getFinalValue(GROUPSPPOINT);
	}
	/**
	 * 邮费
	 * @return
	 * @throws Exception
	 */
	public static double getPostAge() throws Exception {
		return (double)getFinalValue(POSTAGE);
	}
	/**
	 * 免邮费的金额
	 * @return
	 * @throws Exception
	 */
	public static double getPostAgeFree() throws Exception {
		return (double)getFinalValue(POSTAGEFREE);
	}
	/**
	 * 邀请成功奖励的积分
	 * @return
	 * @throws Exception
	 */
	public static double getInviteSpoint() throws Exception {
		return (double)getFinalValue(inviteSpoint);
	}
	/**
	 * 邀请成功奖励的抵扣券
	 * @return
	 * @throws Exception
	 */
	public static double getInviteJkpoint() throws Exception {
		return (double)getFinalValue(inviteJkpoint);
	}
	/**
	 * 邀请成功奖励的惠券
	 * @return
	 * @throws Exception
	 */
	public static double getInviteJypoint() throws Exception {
		return (double)getFinalValue(inviteJypoint);
	}
	/**
	 * 获取注册成功奖励积分
	 * @return
	 * @throws Exception
	 */
	public static double getRegSpoint() throws Exception {
		return (double)getFinalValue(regSpoint);
	}
	
	/**
	 * 获取注册成功奖励的抵扣券
	 * @return
	 * @throws Exception
	 */
	public static double getRegJkpoint() throws Exception {
		return (double)getFinalValue(regJkpoint);
	}
	
	/**
	 * 获取累计签到8天的额外奖励
	 * @return
	 * @throws Exception
	 */
	public static double getSignSpointExtra() throws Exception {
		return (double)getFinalValue(signSpointExtra);
	}
	
	/**
	 * 获取每日签到奖励积分数量
	 * @return
	 * @throws Exception
	 */
	public static double getSignSpoint() throws Exception {
		return (double)getFinalValue(signSpoint);
	}
	
	/**
	 * 获取普通用户升级为黄金会员时要满足的消费金额线
	 */
	public static double getLevel2Line() throws Exception{
		Record config = Db.findFirst("SELECT value FROM t_sys_config WHERE code=?",LEVEL2_LINE_CODE);
		if(config!=null){
			return Double.valueOf(config.getStr("value"));
		}
		throw new Exception("系统配置常量（代码："+LEVEL2_LINE_CODE+"）对应的值找不到!");
	}
	
	
	public static class spikeTime{
		public final static String  SIX  = "6";
		public final static String  EIGHT  = "8";
		public final static String  TEN  = "10";
		public final static String  TWELVE  = "10";
		public final static String  FOURTH  = "14";
		public final static String  SIXTH  = "16";
		public final static String  EIGHTH  = "18";
		public final static String  TWENTY  = "20";
		public final static String  TWENTYTWO  = "22";
		public final static String  ZERO  = "0";
		public final static String  TWO  = "2";
		public final static String  FOUR  = "4";
		
	}
	
	/**
	 * 根据常量字符串获取对应的常量值
	 *@param code 系统常量表中存在的code
	 * @throws Exception 
	 */
	public static Object getFinalValue(String code) throws Exception{
		Record config = Db.findFirst("SELECT value FROM t_sys_config WHERE code=?",code);
		if(config!=null){
			return Double.valueOf(config.getStr("value"));
		}
		throw new Exception("系统配置常量（代码："+code+"）对应的值找不到!");
	}
}
